/**
 * 解析token
 * @param token token
 * @returns {any}
 */
function analyzeToken(token) {
    let strings = token.split(".");
    return JSON.parse(decodeURIComponent(encodeURIComponent(window.atob(strings[1].replace(/-/g, "+").replace(/_/g, "/")))));
}

/**
 * 获取cookie
 * @param name cookie名
 * @returns {string}
 */
function getCookie(name) {
    let cookies = document.cookie.split("; ");
    for (let val of cookies) {
        if (val.split("=")[0] === name) {
            return val.split("=")[1];
        }
    }
}

/**
 * myFetch
 * @param url url
 * @param body 响应体
 * @returns {Promise<Response>}
 */
async function myFetch(url, body) {
    function _getCookie(name) {
        let cookies = document.cookie.split("; ");
        for (let val of cookies) {
            console.log(val.trim());
            if (val.split("=")[0] === name) {
                return val.split("=")[1];
            }
        }
    }

    return await fetch(url, {
        'method': 'POST',
        'headers': {
            "Access-Control-Allow-Origin": '*',
            'Access-Control-Allow-Methods': '*',
            'Access-Control-Allow-Headers': '*',
            'Content-Type': 'application/json',
            'Authorization': _getCookie('authorization')
        },
        'body': JSON.stringify(body)
    });
}

/**
 * 无意义值转null
 * @param data 数据
 * @returns {*|null}
 */
function meaninglessValueToNullValue(data) {
    if (typeof data !== 'object') {
        return data ? data : null;
    }
    for (let key in data) {
        if (typeof data[key] === 'object') {
            data[key] = meaninglessValueToNullValue(data[key]);
            break;
        }
        data[key] = data[key] ? data[key] : null;
    }
    return data;
}